﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLKTX1._0.DAO;
using QLKTX1._0.DTO;

namespace QLKTX1._0.GUI.CauHinh
{
    public partial class FrmKhungGiaNha : Form
    {
        public FrmKhungGiaNha()
        {
            InitializeComponent();
        }

        private void FrmKhungGiaNha_Load(object sender, EventArgs e)
        {
            //--------------Load Khung Gia Nha ----------------------------------------------
            btnkgnLuu.Enabled = false;

            dtgvKhungGiaNha.DataSource = KhungGiaNhaDAO.dsKhungGiaNha();
            //doi ten cot sang tieng viet--------------------------
            dtgvKhungGiaNha.Columns[0].HeaderText = "Mã khung giá";
            dtgvKhungGiaNha.Columns[1].HeaderText = "Mô tả";
            //Chinh do rong cua cot..............
            dtgvKhungGiaNha.Columns[0].Width = 150;
            dtgvKhungGiaNha.Columns[1].Width = 290;
            //Cam chinh sua cot, chi doc.............
            dtgvKhungGiaNha.Columns[0].ReadOnly = true;
            dtgvKhungGiaNha.Columns[1].ReadOnly = true;
            //gan du lieu gridview len textbox-------------------------------
            txtMaKhungGia.DataBindings.Clear();
            txtMaKhungGia.DataBindings.Add("Text", dtgvKhungGiaNha.DataSource, "maKhungGia");
            txtTenKhungGia.DataBindings.Clear();
            txtTenKhungGia.DataBindings.Add("Text", dtgvKhungGiaNha.DataSource, "tenKhungGia");

            //--------------Load Khu ------------------------------------------------------------------

            cmbTenKhu.DataSource = KhuDAO.dsKhu();
            cmbTenKhu.DisplayMember = "tenKhu";
            cmbTenKhu.ValueMember = "maKhu";

            //--------------Load Tang -----------------------------------------------------------------

            cmbTang.DataSource = TangDAO.dsTang();
            cmbTang.DisplayMember = "tenTang";
            cmbTang.ValueMember = "maTang";

            //--------------Load Loai Phong -----------------------------------------------------------

            cmbLoaiPhong.DataSource = LoaiPhongDAO.dsLoaiPhong();
            cmbLoaiPhong.DisplayMember = "tenLoaiPhong";
            cmbLoaiPhong.ValueMember = "maLoaiPhong";

            //--------------Load Khung Gia Nha --------------------------------------------------------

            cmbTenKhungGia.DataSource  = KhungGiaNhaDAO.dsKhungGiaNha();
            cmbTenKhungGia.DisplayMember = "tenKhungGia";
            cmbTenKhungGia.ValueMember = "maKhungGia";

            //--------------Load Chi Tiet Khung Gia Nha -----------------------------------------------
            btnChiTietLuu.Enabled = false;
            dtgvChiTietKGN.DataSource = this.xemChiTietKhungGiaNha();
            // thay doi ten cot
            dtgvChiTietKGN.Columns[0].HeaderText = "Tên khung giá";
            dtgvChiTietKGN.Columns[1].HeaderText = "Khu nhà";
            dtgvChiTietKGN.Columns[2].HeaderText = "Tầng";
            dtgvChiTietKGN.Columns[3].HeaderText = "Loại phòng";
            dtgvChiTietKGN.Columns[4].HeaderText = "Giá thành";
            //Chinh do rong cua cot..............
            dtgvChiTietKGN.Columns[0].Width = 145;
            dtgvChiTietKGN.Columns[1].Width = 85;
            dtgvChiTietKGN.Columns[2].Width = 85;
            dtgvChiTietKGN.Columns[3].Width = 105;
            dtgvChiTietKGN.Columns[4].Width = 100;
            //Cam chinh sua cot, chi doc.............
            dtgvChiTietKGN.Columns[0].ReadOnly = true;
            dtgvChiTietKGN.Columns[1].ReadOnly = true;
            dtgvChiTietKGN.Columns[2].ReadOnly = true;
            dtgvChiTietKGN.Columns[3].ReadOnly = true;
            dtgvChiTietKGN.Columns[4].ReadOnly = true;
            //gan du lieu gridview len textbox-------------------------------
            cmbTenKhungGia.DataBindings.Clear();
            cmbTenKhungGia.DataBindings.Add("Text", dtgvChiTietKGN.DataSource, "maKhungGia");
            cmbTenKhu.DataBindings.Clear();
            cmbTenKhu.DataBindings.Add("Text", dtgvChiTietKGN.DataSource, "maKhu");
            cmbTang.DataBindings.Clear();
            cmbTang.DataBindings.Add("Text", dtgvChiTietKGN.DataSource, "maTang");
            cmbLoaiPhong.DataBindings.Clear();
            cmbLoaiPhong.DataBindings.Add("Text", dtgvChiTietKGN.DataSource, "maLoaiPhong");
            txtDonGia.DataBindings.Clear();
            txtDonGia.DataBindings.Add("Text", dtgvChiTietKGN.DataSource, "donGia");

            lbChiTietKGN.Text = "Tổng số danh mục là:" +ChiTietKhungGiaNhaDAO .dsChiTietKhungGiaNha ().Count .ToString ();
            
             
        }

        private DataTable xemChiTietKhungGiaNha()
        {
            DataTable dt = new DataTable(); // bang de chua

            DataColumn col1 = new DataColumn("maKhungGia");
            dt.Columns.Add(col1);

            DataColumn col2 = new DataColumn("maKhu");
            dt.Columns.Add(col2);            
            
            DataColumn col3 = new DataColumn("maTang");
            dt.Columns.Add(col3);

            DataColumn col4 = new DataColumn("maLoaiPhong");
            dt.Columns.Add(col4);

            DataColumn col5 = new DataColumn("donGia");
            dt.Columns.Add(col5);

            foreach (ChiTietKhungGiaNhaDTO  dot in ChiTietKhungGiaNhaDAO .dsChiTietKhungGiaNha ())
            {
                dt.AcceptChanges();

                DataRow dr = dt.NewRow(); // tao 1 hang

                dr[0] = dot.MaKhungGia.TenKhungGia;                
                dr[1] = dot.MaKhu.TenKhu ;
                dr[2] = dot.MaTang.TenTang;
                dr[3] = dot.MaLoaiPhong.TenLoaiPhong ;               
                dr[4] = dot.DonGia ;               

                dt.Rows.Add(dr);

            }
            return dt;
        }

        private void btnkgnThem_Click(object sender, EventArgs e)
        {
            if (btnkgnThem.Text.Equals("Thêm"))
            {
                btnkgnCapNhat.Enabled = false;
                btnkgnXoa.Enabled = false;
                btnkgnLuu.Enabled = true;
                //txtMaKhungGia.Text = Service.generateNoAuto("khungGiaNha", "maKhungGia", "1", 1);
                txtMaKhungGia.Enabled = false;
                txtTenKhungGia.Text = "";

                btnkgnThem.Text = "Hủy bỏ";

            }
            else
            {
                btnkgnThem.Text = "Thêm";
                btnkgnCapNhat.Enabled = true;
                btnkgnXoa.Enabled = true;
                btnkgnLuu.Enabled = false;
                this.FrmKhungGiaNha_Load(sender, e);
            }
        }

        private void btnkgnLuu_Click(object sender, EventArgs e)
        {
            if (btnkgnThem.Text.Equals("Hủy bỏ"))
            {
                if (txtTenKhungGia.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
               

                    KhungGiaNhaDTO kgn = new KhungGiaNhaDTO();
                    kgn.MaKhungGia = Int32.Parse (txtMaKhungGia.Text);
                    kgn.TenKhungGia = txtTenKhungGia.Text ;

                    KhungGiaNhaDAO.themKhungGianNha(kgn);

                    btnkgnThem.Text = "Thêm";
                    btnkgnCapNhat.Enabled = true;
                    btnkgnXoa.Enabled = true;
                    btnkgnThem.Enabled = true;
                    this.FrmKhungGiaNha_Load(sender, e);
                }
            }
            else
            {
                if (txtTenKhungGia.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    KhungGiaNhaDTO kgn = new KhungGiaNhaDTO();
                    kgn.MaKhungGia = Int32.Parse(txtMaKhungGia.Text);
                    kgn.TenKhungGia = txtTenKhungGia.Text;

                    KhungGiaNhaDAO.capNhatKhungGianNha(kgn);

                    btnkgnCapNhat.Text = "Cập Nhật";
                    btnkgnCapNhat.Enabled = true;
                    btnkgnXoa.Enabled = true;
                    btnkgnThem.Enabled = true;
                    this.FrmKhungGiaNha_Load(sender, e);
                }
            }
        }

        private void btnkgnCapNhat_Click(object sender, EventArgs e)
        {
            if (btnkgnCapNhat.Text.Equals("Cập Nhật"))
            {
                btnkgnLuu.Enabled = true;
                btnkgnThem.Enabled = false;
                btnkgnXoa.Enabled = false;
                btnkgnCapNhat.Text = "Hủy bỏ";
                // this.FrmLoaiChiPhi_Load(sender, e);

            }
            else
            {
                btnkgnLuu.Enabled = false;
                btnkgnThem.Enabled = true;
                btnkgnXoa.Enabled = true;

                btnkgnCapNhat.Text = "Cập Nhật";
                this.FrmKhungGiaNha_Load(sender, e);
            }
        }

        private void btnkgnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa ?", "Xác nhận xóa ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                KhungGiaNhaDTO kgn = new KhungGiaNhaDTO();
                kgn.MaKhungGia = Int32.Parse(txtMaKhungGia.Text);
                kgn.TenKhungGia = txtTenKhungGia.Text;

                KhungGiaNhaDAO.xoaKhungGianNha(kgn);


                this.FrmKhungGiaNha_Load(sender, e);
            }
        }

        private void btnChiTietThem_Click(object sender, EventArgs e)
        {
            if (btnChiTietThem.Text.Equals("Thêm"))
            {
                btnChiTietCapNhat.Enabled = false;
                btnChiTietXoa.Enabled = false;
                btnChiTietLuu.Enabled = true;

            
                txtDonGia.Text = "";

                btnChiTietThem.Text = "Hủy bỏ";

            }
            else
            {
                btnChiTietThem.Text = "Thêm";
                btnChiTietCapNhat.Enabled = true;
                btnChiTietXoa.Enabled = true;
                btnChiTietLuu.Enabled = false;
                this.FrmKhungGiaNha_Load(sender, e);
            }
        }

        private void btnChiTietLuu_Click(object sender, EventArgs e)
        {
            if (btnChiTietThem.Text.Equals("Hủy bỏ"))
            {
                if (txtDonGia.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                   
                    ChiTietKhungGiaNhaDTO ctkgn = new ChiTietKhungGiaNhaDTO();
                    ctkgn.DonGia = Int32.Parse(txtDonGia.Text);

                    string t = cmbTang.SelectedValue.ToString();
                    TangDTO tang = new TangDTO();
                    tang.MaTang = t;
                    ctkgn.MaTang = tang;

                    string khu = cmbTenKhu.SelectedValue.ToString();
                    KhuDTO k = new KhuDTO();
                    k.MaKhu = khu;
                    ctkgn.MaKhu = k;

                    string loaiphong = cmbLoaiPhong.SelectedValue.ToString();
                    LoaiPhongDTO lp = new LoaiPhongDTO();
                    lp.MaLoaiPhong = loaiphong;
                    ctkgn.MaLoaiPhong = lp;

                    int khungGiaNha = Int32.Parse (cmbTenKhungGia.SelectedValue.ToString());
                    KhungGiaNhaDTO kgn = new KhungGiaNhaDTO();
                    kgn.MaKhungGia = khungGiaNha;
                    ctkgn.MaKhungGia = kgn;


                    if (ChiTietKhungGiaNhaDAO.isExistData(ctkgn))
                    {

                                FrmThongBao f = new FrmThongBao("Da co roi!");

                                f.StartPosition = FormStartPosition.CenterScreen;

                                f.TopLevel = true;

                                f.ShowDialog();

                    }
                    else ChiTietKhungGiaNhaDAO.themChiTietKhungGiaNha(ctkgn);
                    
                                        
                 
                    btnChiTietThem.Text = "Thêm";
                    btnChiTietCapNhat.Enabled = true;
                    btnChiTietXoa.Enabled = true;
                    btnChiTietThem.Enabled = true;
                    this.FrmKhungGiaNha_Load(sender, e);
                }
            }
            else
            {
                if (txtDonGia.Text.Equals(""))
                {
                    MessageBox.Show("Vui lòng nhập dữ liệu", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    ChiTietKhungGiaNhaDTO ctkgn = new ChiTietKhungGiaNhaDTO();
                    ctkgn.DonGia = Int32.Parse(txtDonGia.Text);

                    string t = cmbTang.SelectedValue.ToString();
                    TangDTO tang = new TangDTO();
                    tang.MaTang = t;
                    ctkgn.MaTang = tang;

                    string khu = cmbTenKhu.SelectedValue.ToString();
                    KhuDTO k = new KhuDTO();
                    k.MaKhu = khu;
                    ctkgn.MaKhu = k;

                    string loaiphong = cmbLoaiPhong.SelectedValue.ToString();
                    LoaiPhongDTO lp = new LoaiPhongDTO();
                    lp.MaLoaiPhong = loaiphong;
                    ctkgn.MaLoaiPhong = lp;

                    int khungGiaNha = Int32.Parse(cmbTenKhungGia.SelectedValue.ToString());
                    KhungGiaNhaDTO kgn = new KhungGiaNhaDTO();
                    kgn.MaKhungGia = khungGiaNha;
                    ctkgn.MaKhungGia = kgn;

                    /*
                    if (ValidateData.checkDataExistabled("Tang", "maTang", t) && ValidateData.checkDataExistabled("Khu", "maKhu", khu) && ValidateData.checkDataExistabled("loaiPhong", "maLoaiPhong", loaiphong) && ValidateData.checkDataExistabled("khungGiaNha", "maKhungGia", khungGiaNha.ToString()))
                    {

                        FrmThongBao f = new FrmThongBao("Da co roi");

                        f.StartPosition = FormStartPosition.CenterScreen;

                        f.TopLevel = true;

                        f.ShowDialog();

                    }else  */
                        
                    ChiTietKhungGiaNhaDAO.capNhatChiTietKhungGiaNha(ctkgn);
                 

                    btnChiTietCapNhat.Text = "Cập Nhật";
                    btnChiTietCapNhat.Enabled = true;
                    btnChiTietXoa.Enabled = true;
                    btnChiTietThem.Enabled = true;
                    this.FrmKhungGiaNha_Load(sender, e);
                }
            }
        }

        private void btnChiTietCapNhat_Click(object sender, EventArgs e)
        {
            if (btnChiTietCapNhat.Text.Equals("Cập Nhật"))
            {
                btnChiTietLuu.Enabled = true;
                btnChiTietThem.Enabled = false;
                btnChiTietXoa.Enabled = false;
                btnChiTietCapNhat.Text = "Hủy bỏ";
                

            }
            else
            {
                btnChiTietLuu.Enabled = false;
                btnChiTietThem.Enabled = true;
                btnChiTietXoa.Enabled = true;

                btnChiTietCapNhat.Text = "Cập Nhật";
                this.FrmKhungGiaNha_Load(sender, e);
            }
        }

        private void btnChiTietXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc muốn xóa ?", "Xác nhận xóa ", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                ChiTietKhungGiaNhaDTO ctkgn = new ChiTietKhungGiaNhaDTO();
                //ctkgn.DonGia = Int32.Parse(txtDonGia.Text);

                string t = cmbTang.SelectedValue.ToString();
                TangDTO tang = new TangDTO();
                tang.MaTang = t;
                ctkgn.MaTang = tang;

                string khu = cmbTenKhu.SelectedValue.ToString();
                KhuDTO k = new KhuDTO();
                k.MaKhu = khu;
                ctkgn.MaKhu = k;

                string loaiphong = cmbLoaiPhong.SelectedValue.ToString();
                LoaiPhongDTO lp = new LoaiPhongDTO();
                lp.MaLoaiPhong = loaiphong;
                ctkgn.MaLoaiPhong = lp;

                int khungGiaNha = Int32.Parse(cmbTenKhungGia.SelectedValue.ToString());
                KhungGiaNhaDTO kgn = new KhungGiaNhaDTO();
                kgn.MaKhungGia = khungGiaNha;
                ctkgn.MaKhungGia = kgn;

                ChiTietKhungGiaNhaDAO.xoaChiTietKhungGiaNha(ctkgn);
                
                this.FrmKhungGiaNha_Load(sender, e);
            }
        }


    }
}
